#!/usr/bin/env ruby

$LOAD_PATH << "../lib"

require "starruby"
require "starframe/sprite"
include StarRuby

class StarSprite < StarFrame::Sprite
  @texture = Texture.load("images/star")
  MAX_X = 320 - @texture.width
  MAX_Y = 240 - @texture.height
  
  def init *args
    @x = rand(MAX_X)
    @y = rand(MAX_Y)
    @movement_x = rand(2) * 2 - 1
    @movement_y = rand(2) * 2 - 1
  end
  
  def update
    if @x < 0
      @x = -@x
      @movement_x = 1
    end
    if @y < 0
      @y = -@y
      @movement_y = 1
    end
    if MAX_X <= @x
      @x = -(@x - MAX_X) + MAX_X
      @movement_x = -1
    end
    if MAX_Y <= @y
      @y = -(@y - MAX_Y) + MAX_Y
      @movement_y = -1
    end
  end
end

sprites = Array.new(200) {StarSprite.new}

Game.run(320, 240, :title => "Sprites (With StarFrame::Sprite)") do |game|
  break if Input.keys(:keyboard).include?(:escape)
  if Input.keys(:mouse).include?(:left)
    game.fps = 100000
  else
    game.fps = 30
  end
  sprites.each do |sprite|
    sprite.call
  end
  game.screen.clear
  sprites.each do |sprite|
    sprite.render_to game.screen
  end
end
